package com.GPT.common;

import javax.servlet.http.HttpServletRequest;

public class UserId {
    public Long getUserId(HttpServletRequest request){
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String token = httpRequest.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 去掉 "Bearer " 前缀
        }
        Long userId = null;
        // 如果 token 存在且有效
        if (token != null && Jwt.validateToken(token)) {
            // 获取 token 的声明
            userId = Jwt.getUserId(token);
        }
        return userId;
    }
}
